<?php
declare(strict_types=1);
namespace Service\CommodityAttributeService;

use App\Model\CommodityModel\Classifyinfo;

class ClassInfoService extends CommodityAttributeBaseService
{
    private $redisKey = 'ClassifyMap';

    public function getArchiveIds($name)
    {
        $redis = $this->getRedisObj();
        $classifyMapJson = $redis->get($this->redisKey);
        $tableName = Classifyinfo::instance()->getTable();
        if (!$classifyMapJson) {
            $classifyData = Classifyinfo::instance()
                ->from($tableName . ' c')
                ->select([
                    'CONCAT(c.ID,"") AS ID', 'CONCAT(c.ParentID,"") AS ParentID',
                    'c.ClassificationName', 'CONCAT(p.ParentID,"") AS GrandID'
                ])
                ->join($tableName . ' p', 'p.ID', '=', 'c.ParentID')
                ->where('c.Level', 3)
                ->get()->toArray();
            $classifyDataMap = array_column($classifyData, NULL, 'ClassificationName');
            //保存到redis
            $redis->set($this->redisKey, json_encode($classifyDataMap));
        } else {
            $classifyDataMap = json_decode($classifyMapJson, true);
        }
        if (isset($classifyDataMap[$name])) {
            return [
                'FirstClassificationID' => $classifyDataMap[$name]['GrandID'],
                'SecondClassificationID' => $classifyDataMap[$name]['ParentID'],
                'ThirdClassificationID' => $classifyDataMap[$name]['ID'],
            ];
        }
        return [
            'FirstClassificationID' => 0,
            'SecondClassificationID' => 0,
            'ThirdClassificationID' => 0,
        ];
    }

}